package com.lagou.controller;

import com.lagou.constant.Result;
import com.lagou.utils.ResultUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/9/17
 */
@Controller
public class LoginController {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";

    private static final String TOKEN_NAME = "token";
    private static final String TOKEN_VALUE = "ASDQ23EQ21313A";

    @RequestMapping("/valid")
    @ResponseBody
    public Result login(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletResponse response) {
        if (USERNAME.equals(username) && PASSWORD.equals(password)) {
            Cookie cookie = new Cookie(TOKEN_NAME, TOKEN_VALUE);
            response.addCookie(cookie);
            return ResultUtils.success();
        }
        return ResultUtils.error(-1, "用户名或密码错误!");
    }

    @RequestMapping("/login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}
